﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PurchaseManagementSystem
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 登陆按钮操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_login_Click(object sender, EventArgs e)
        {
            //用户名，密码，登陆类型都不为空
            if (CheckInput())
            {
                string message = string.Empty;//表示验证消息
                int Employee_department = CheckUser(ref message);
                //检索用户名，密码是否匹配
                if (Employee_department == 1 || Employee_department == 2)
                {
                    //实例化登陆对象
                    LoginUser loginuser = new LoginUser();

                    if (Employee_department == 1)
                    {
                        //进入生产部
                        Add_Order add_order = new Add_Order();
                        //数据传递
                        loginuser.UserAccount = this.txt_username.Text.Trim();
                        loginuser.UserPwd = this.txt_userpwd.Text.Trim();
                        //生产部id==1
                        loginuser.LogintDepartment = 1;

                        //MessageBox.Show("生产部员工登陆成功");
                        add_order.Show();
                    }
                    else if (Employee_department == 2)
                    {
                        //进入销售部
                        purchase_option po = new purchase_option();
                        //数据传递
                        loginuser.UserAccount = this.txt_username.Text.Trim();
                        loginuser.UserPwd = this.txt_userpwd.Text.Trim();
                        loginuser.LogintDepartment = 2;
                        //MessageBox.Show("采购部员工登陆成功");
                        po.Show();
                    }
                    //隐藏登陆窗体
                    this.Hide();
                }
                else
                {
                    // 弹出提示消息
                    MessageBox.Show(message, "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
        /// <summary>
        /// 窗口载入加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        /// <summary>
        /// 取消按钮操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_cancel_Click(object sender, EventArgs e)
        {
            //关闭当前窗口
            this.Close();
        }

        /// <summary>
        /// 用户名、密码的非空验证
        /// </summary>
        /// <returns>True都不为空，False其中一个为空</returns>
        public bool CheckInput()
        {
            //用户名为空
            if (this.txt_username.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入用户名", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txt_username.Focus();
                return false;
            }

            //密码为空
            else if (this.txt_userpwd.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入密码", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txt_userpwd.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 分别检查用户名、密码是否存在
        /// </summary>        
        /// <param name="message">提示的消息</param>
        /// <returns>员工的部门</returns>
        public int CheckUser(ref string message)
        {
            int isValidUser = 0; // 表示验证是否通过 0 :未通过 1:生产部 2:采购部
            string userAccount = this.txt_username.Text.Trim(); // 输入的用户名
            string userPwd = this.txt_userpwd.Text.Trim();  // 输入的密码
            isValidUser = DBServer.Find_department(userAccount, userPwd);
            if (isValidUser == 0)
                message = "用户名不存在";
            else if (isValidUser == -1)
                message = "密码错误";
            return isValidUser;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
